<?php
   /***************************************************************/
   /* SmpRSS - a library for fetching and parsing RSS feeds
      by Petko D. (pdp) Petkov                                    */
   /***************************************************************/

   /***************************************************************/
   /* Http - a class for making HTTP requests
   
      Software License Agreement (BSD License)
   
      Copyright (C) 2005-2007, Edward Eliot.
      All rights reserved.
      
      Redistribution and use in source and binary forms, with or without
      modification, are permitted provided that the following conditions are met:

         * Redistributions of source code must retain the above copyright
           notice, this list of conditions and the following disclaimer.
         * Redistributions in binary form must reproduce the above copyright
           notice, this list of conditions and the following disclaimer in the
           documentation and/or other materials provided with the distribution.
         * Neither the name of Edward Eliot nor the names of its contributors 
           may be used to endorse or promote products derived from this software 
           without specific prior written permission of Edward Eliot.

      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND ANY
      EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
      WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
      DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
      (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
      LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
      ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
      SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   
      Last Updated:  7th January 2007                             */
   /***************************************************************/
   
   define('HTTP_TIMEOUT', 3); // how long to wait for a connection before aborting, CURL only
   define('MAX_HTTP_REQUEST_TIME', 5); // maximum time allowed for completing URL request before aborting, CURL only
   define('HTTP_USERAGENT', 'SmpRSS');
   
   class SmpRSSHttp {
      var $iConnectTimeout;
      var $iRequestTimeout;
      var $sUserAgent;
      
      function SmpRSSHttp($iConnectTimeout = HTTP_TIMEOUT, $iRequestTimeout = MAX_HTTP_REQUEST_TIME, $sUserAgent = HTTP_USERAGENT) {
         $this->iConnectTimeout = $iConnectTimeout;
         $this->iRequestTimeout = $iRequestTimeout;
      }
      
      function Get($sUrl) {
         // check for curl lib, use in preference to file_get_contents if available
         if (function_exists('curl_init')) {
            // initiate session
            $oCurl = curl_init($sUrl);
            // set options
            curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->iConnectTimeout);
            curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->iRequestTimeout);
            curl_setopt($oCurl, CURLOPT_USERAGENT, $this->sUserAgent);
            curl_setopt($oCurl, CURLOPT_HEADER, false);
            curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
            // request URL
            $sResult = curl_exec($oCurl);
            // close session
            curl_close($oCurl);
            return $sResult;
         } else {
            ini_set('user_agent', HTTP_USERAGENT);
            // fopen_wrappers need to be enabled for this to work - see http://www.php.net/manual/en/function.file-get-contents.php
            if ($sResult = @file_get_contents($sUrl)) {
               return $sResult;
            }
         }
         return false;
      }
   }

   /***************************************************************/
   /* SimpleRss - a class fetching and parsing RSS feeds
   
      Software License Agreement (BSD License)
   
      Copyright (C) 2005-2007, Edward Eliot.
      All rights reserved.
      
      Redistribution and use in source and binary forms, with or without
      modification, are permitted provided that the following conditions are met:

         * Redistributions of source code must retain the above copyright
           notice, this list of conditions and the following disclaimer.
         * Redistributions in binary form must reproduce the above copyright
           notice, this list of conditions and the following disclaimer in the
           documentation and/or other materials provided with the distribution.
         * Neither the name of Edward Eliot nor the names of its contributors 
           may be used to endorse or promote products derived from this software 
           without specific prior written permission of Edward Eliot.

      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND ANY
      EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
      WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
      DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
      (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
      LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
      ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
      SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   
      Last Updated:  7th January 2007                             */
   /***************************************************************/
   
   // required files
   require dirname(__FILE__).'/__c.YCache.php';
   
   // RssObject, RssChannel and RssItem are support objects which represent the structure of the returned data
   class RssObject {
      var $oChannel;
      var $aItems = array();
      
      function RssObject() {
         $this->oChannel = new RssChannel();
      }
   }
   
   class RssChannel {
      var $sTitle = '';
      var $sDescription = '';
      var $sLink = '';
      var $sDate = '';
      var $sGenerator = '';
      var $sLanguage = '';
   }
   
   class RssItem {
      var $sTitle = '';
      var $sDescription = '';
      var $sLink = '';
      var $sDate = '';
      var $sAuthor = '';
      var $sCategory = '';
   }
   
   // main class
   class SmpRSS {
      var $oRssObject;
      var $iNumItems;
      var $sInputEncoding;
      var $sOutputEncoding;
      var $bInItem = false;
      var $bInChannel = false;
      var $sTag = '';
      var $iPointer = -1;
      var $sTempContent = '';
      var $bSuccessful = false;
      var $bCached = false;
      var $bStaleCache = false;
      
      // $vCacheTime can be set to false to disable caching
      // $iNumItems = -1 means whatever's in the feed
      // sInputEncoding - in PHP 4 default is ISO-8859-1, in PHP 5 detected automatically
      // sOutputEncoding - defaults to UTF-8
      function SmpRSS($sUrl, $vCacheTime = 300, $iNumItems = -1, $sInputEncoding = '', $sOutputEncoding = 'UTF-8') {
         $this->oRssObject = new RssObject(); // this object holds the returned data
         $this->iNumItems = $iNumItems;
         $this->sInputEncoding = $sInputEncoding;
         $this->sOutputEncoding = $sOutputEncoding;
         
         // make sure caching hasn't been disabled
         if ($vCacheTime) {
            $oCache = new YCache($sUrl.'_'.$this->iNumItems, $vCacheTime);
         }
         
         // is caching disabled or if not has the cache expired
         if (!$vCacheTime || !$oCache->check()) {
            $oHttp = new SmpRSSHttp();
            
            // request feed
            if ($sData = $oHttp->Get($sUrl)) {
               $this->bSuccessful = $this->Parse($sData);
            
               // do we want to cache result
               if ($vCacheTime) {
                  $oCache->set($this->oRssObject);
               }
            }
         } else {
            // get data from cache
            $this->oRssObject = $oCache->get();
            $this->bSuccessful = true;
            $this->bCached = true;
         }
         
         // check to see if request was successful, if not try and retrieve stale cache
         if (!$this->bSuccessful && $vCacheTime && $oCache->exists()) {
            // make cache fresh
            $oCache->revalidate();
            // get data from cache
            $this->oRssObject = $oCache->get();
            $this->bSuccessful = true;
            $this->bCached = true;
            // mark as stale request
            $this->bStaleCache = true;
         }
      }
      
      function Parse($sData) {
         // set up XML parser
         if ($this->sInputEncoding != '' && version_compare(phpversion(), '5', '<')) {
            $oParser = xml_parser_create($this->sInputEncoding);
         } else {
            $oParser = xml_parser_create();
         }
         xml_parser_set_option($oParser, XML_OPTION_TARGET_ENCODING, $this->sOutputEncoding);
         // set scope of handler functions to this class
         xml_set_object($oParser, $this);
         // set handler functions
         xml_set_element_handler($oParser, 'StartTag', 'CloseTag');
         xml_set_character_data_handler($oParser, 'TagContent');
         // parse the data, set flag to indicate success or failure
         $bResult = xml_parse($oParser, $sData);
         // free memory used
         xml_parser_free($oParser);
         
         return $bResult;
      }
      
      // this function triggers each time the parser encounters a new tag
      function StartTag($oParser, $sName, $aAttributes) {
         if ($this->bInItem || ($this->bInChannel && $sName != 'ITEM')) {
            $this->sTag = $sName;
         } else {
            switch ($sName) {
               case 'ITEM':
                  $this->bInItem = true;
                  $this->iPointer++;
                  
                  if ($this->iNumItems == -1 || $this->iPointer < $this->iNumItems) {
                     $this->oRssObject->aItems[] = new RssItem();
                  }
                  break;
               case 'CHANNEL':
                  $this->bInChannel = true;
                  break;
            }
         }
      }
      
      // this function triggers when the parser encounters a corresponding close tag
      function CloseTag($oParser, $sName) {
         if ($sName == 'ITEM') {
            if ($this->iNumItems == -1 || $this->iPointer < $this->iNumItems) {
               // if the feed contained a content element we'll override the description text with it as it's 
               // likely to represent the entire article
               if ($this->sTempContent != '') {
                  $this->oRssObject->aItems[$this->iPointer]->sDescription = $this->sTempContent;
                  $this->sTempContent = '';
               }
            }
            $this->bInItem = false;
         } elseif ($sName == 'CHANNEL') {
            $this->bInChannel = false;
         }
      }
      
      // this function triggers when the parser encounters content for the current tag
      function TagContent($oParser, $sData) {
         // is the parser looking at an item
         if ($this->bInItem) {
            if ($this->iNumItems == -1 || $this->iPointer < $this->iNumItems) {
               switch ($this->sTag) {
                  case 'TITLE':
                     $this->oRssObject->aItems[$this->iPointer]->sTitle .= $sData;
                     break;
                  case 'DESCRIPTION':
                     $this->oRssObject->aItems[$this->iPointer]->sDescription .= $sData;
                     break;
                  case 'CONTENT:ENCODED':
                     $this->sTempContent .= $sData;
                     break;
                  case 'LINK':
                     $this->oRssObject->aItems[$this->iPointer]->sLink .= $sData;
                     break;
                  case 'PUBDATE':
                  case 'DC:DATE':
                     $this->oRssObject->aItems[$this->iPointer]->sDate .= $sData;
                     break;
                  case 'AUTHOR':
                  case 'DC:CREATOR':
                     $this->oRssObject->aItems[$this->iPointer]->sAuthor .= $sData;
                     break;
                  case 'CATEGORY':
                     $this->oRssObject->aItems[$this->iPointer]->sCategory .= $sData;
                     break;
                  
               }
            }
         } elseif ($this->bInChannel) { // is the parser looking at global channel data
            switch ($this->sTag) {
               case 'TITLE':
                  $this->oRssObject->oChannel->sTitle .= $sData;
                  break;
               case 'DESCRIPTION':
                  $this->oRssObject->oChannel->sDescription .= $sData;
                  break;
               case 'LINK':
                  $this->oRssObject->oChannel->sLink .= $sData;
                  break;
               case 'PUBDATE':
                  $this->oRssObject->oChannel->sDate .= $sData;
                  break;
               case 'GENERATOR':
                  $this->oRssObject->oChannel->sGenerator .= $sData;
                  break;
               case 'LANGUAGE':
                  $this->oRssObject->oChannel->sLanguage .= $sData;
                  break;
            }
         }
      }
      
      // gets the returned feed data structure
      function GetRssObject() {
         if ($this->bSuccessful) {
            return $this->oRssObject;
         } else {
            return false;
         }
      }
      
      // indicates whether the feed data was retrieved from cache
      function IsCached() {
         return $this->bCached;
      }
      
      // indicates whether the cached data returned was stale - therefore an http request to get live data failed
      function IsStaleCache() {
         return $this->bStaleCache;
      }
   }
?>
